package com.serverlite.framework.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * function:Get properties file attributes value author：shaojiang date：2011.7.20
 */
public class PropertiesUtils {

    private static Properties pros = null;// Properties Object

    static {
        pros = new Properties();// Create a Properties object
    }

    /**
     * Loading files to Properties object
     * 
     * @param fileName
     */
    public static void load(String fileName) {
        InputStream in = PropertiesUtils.class.getResourceAsStream(fileName);
        try {
            pros.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String loadValue(String fileName, String key) {
        load(fileName);
        return get(key);
    }

    public static String get(String key) {
        try {
            if (pros.getProperty(key) != null) {
                return new String(pros.getProperty(key).getBytes("ISO8859-1"),
                        "UTF-8");
            } else {
                return "";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String[] loadStrArray(String fileName, String key) {
        load(fileName);
        return getStrArray(key);
    }

    public static String[] getStrArray(String key) {
        return get(key).split(",");
    }
}
